package com.cmy.b_claculator.db.dao

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.room.*
import com.cmy.b_claculator.db.entity.*
import com.cmy.b_claculator.model.WorkModel

@Dao
interface WorkDateDao {
    /**
     * 新增单位名称
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertWorkUnit(unitEntity: WorkUnitEntity):Long

    /**
     * 新增分类
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertWorkSpecies(speciesEntity:WorkSpeciesEntity):Long


    /**
     * 新增工作信息
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertWork(work:WorkEntity):Long

    /**
     * 新增工作日期信息
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertWorkDate(workData: WorkDataEntity):Long

    /**
     * 查询所有
     */
    @Query("SELECT * FROM WorkDataEntity ORDER BY ID DESC")
    fun getAllWorkDate():LiveData<MutableList<WorkDataEntity>>

    @Query("SELECT * FROM WorkDataEntity WHERE time = (:time) ")
    fun queryWorkDate( time:String):WorkDataEntity?

    @Query("SELECT * FROM WorkDataEntity WHERE WorkDataEntity.id = (:id) ")
    fun queryWorkDateById( id:Long):WorkDataEntity?

    @Query("SELECT * FROM WorkEntity WHERE WorkEntity.id = (:id) ")
    fun queryWorkById( id:Long):WorkEntity?

    @Query("SELECT * FROM WorkEntity ORDER BY ID DESC")
    fun queryAllWork( ):MutableList<WorkEntity>

    /**
     * 修改金额
     */
    @Update(entity = WorkDataEntity::class)
    fun upDateWorkDateMoney(type: WorkDataMoney)


    /**
     * 修改状态
     */
    @Update(entity = WorkDataEntity::class)
    fun upDateWorkDateType(type: WorkDataType)


    /**
     * 批量修改状态
     */
    @Update(entity = WorkDataEntity::class)
    fun upDateWorkDateTypes(types: MutableList<WorkDataType>)

    /**
     * 模糊查询对应种类
     */
    @Query("SELECT * FROM WorkSpeciesEntity WHERE name LIKE '%' || :str || '%'")
    fun queryLikeSpecies(str:String):MutableList<WorkSpeciesEntity>?
    /**
     * 查询对应种类
     */
    @Query("SELECT * FROM WorkSpeciesEntity WHERE name =:str ORDER BY ID DESC")
    fun querySpecies(str:String):WorkSpeciesEntity?

    /**
     * 查询对应种类
     */
    @Query("SELECT * FROM WorkSpeciesEntity WHERE WorkSpeciesEntity.id =:id ORDER BY ID DESC")
    fun querySpeciesById(id:Long):WorkSpeciesEntity?

    /**
     * 模糊查询对应种类
     */

    @Query("SELECT * FROM  WorkUnitEntity WHERE name LIKE ('%' || :str || '%')")
    fun queryLikeUnit(str:String):MutableList<WorkUnitEntity>?

    /**
     * 查询对应种类
     */

    @Query("SELECT * FROM  WorkUnitEntity WHERE name = :str  ORDER BY ID DESC")
    fun queryUnit(str:String):WorkUnitEntity?

    /**
     * 查询对应种类
     */
    @Query("SELECT * FROM  WorkUnitEntity WHERE WorkUnitEntity.id = :id  ORDER BY ID DESC")
    fun queryUnitById(id:Long):WorkUnitEntity?


    /**
     * 多表联查
     */
    @Query("SELECT WorkEntity.id,WorkEntity.price,WorkEntity.number,WorkUnitEntity.name as unitName,WorkSpeciesEntity.name as speciesName FROM  WorkEntity INNER JOIN WorkUnitEntity,WorkSpeciesEntity ON WorkUnitEntity.id = WorkEntity.unitId AND WorkSpeciesEntity.id = WorkEntity.speciesId  WHERE WorkEntity.dataId = :id")
    fun queryWork(id:Long):MutableList<WorkModel>

}